package com.base.study.activity;


import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.recy.BasicRecyViewHolder;
import com.base.commonlib.recy.utils.CustomDivider;
import com.base.commonlib.recy.utils.SpaceItemDecoration;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.commonlib.sp.SpUtil;
import com.base.study.R;
import com.base.study.adapter.PopualSearchAdapter;
import com.base.study.bean.EsSearchResultBean;
import com.base.study.bean.SearchResultBean;
import com.base.study.databinding.ActivityPopualSearchBinding;
import com.base.study.presenter.PopualSearchPresenter;
import com.base.study.view.PopualSearchView;
import java.util.ArrayList;
import java.util.List;

import co.lujun.androidtagview.ColorFactory;
import co.lujun.androidtagview.TagView;


/**
 * 热门搜索
 * Created by zw on 2020/2/13.
 */

@Route(path = "/mystudy/search")
public class PopualSearchActivity extends CBaseBarMvpActivity<PopualSearchPresenter> implements PopualSearchView {

    private ActivityPopualSearchBinding mBinding;
    ArrayList<String> tents = new ArrayList<>();
    private int index = 1;
    @Autowired
    String kewWord;
    private PopualSearchAdapter mAdapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_popual_search;
    }

    @Override
    protected void init() {
        super.init();
        setLightStatusBar(this, true);
        mBinding = (ActivityPopualSearchBinding) mRootBinding;
        mPresenter = new PopualSearchPresenter(this,this);
        mPresenter.start();
        mBinding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.refreshLayout.setHeaderHeight(60);
        mBinding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        if (!TextUtils.isEmpty(kewWord)){
            mBinding.search.setText(kewWord);
            mBinding.refreshLayout.autoRefresh();
            mBinding.llPopual.setVisibility(View.GONE);
            mBinding.llResult.setVisibility(View.VISIBLE);
        }else {
            mBinding.llPopual.setVisibility(View.VISIBLE);
            mBinding.llResult.setVisibility(View.GONE);
        }
    }

    @Override
    protected void start() {
        tents.add("城市管理");
        tents.add("垃圾分类");
        tents.add("环境卫生");
        tents.add("城管执法");
        mBinding.tagcontainerLayout.setTheme(ColorFactory.PURE_CYAN);
        ImageView imageView=new ImageView(mContext);
        imageView.setImageResource(R.mipmap.ic_logo);
        mBinding.tagcontainerLayout.addView(imageView);
        mBinding.tagcontainerLayout.setTheme(ColorFactory.NONE);
        mBinding.tagcontainerLayout.setTagBorderColor(Color.TRANSPARENT);
        mBinding.tagcontainerLayout.setTagBackgroundColor(Color.parseColor("#f4f4f4"));
        mBinding.tagcontainerLayout.setTags(tents);
        mBinding.tagcontainerLayout.setOnTagClickListener(new TagView.OnTagClickListener() {
            @Override
            public void onTagClick(int position, String text) {
                kewWord = text;
                mBinding.search.setText(text);
                mBinding.refreshLayout.autoRefresh();
            }

            @Override
            public void onTagLongClick(int position, String text) {

            }
        });


        mBinding.search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    index = 1;
                    kewWord = mBinding.search.getText().toString().trim();
                    mBinding.refreshLayout.autoRefresh();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                        if (getCurrentFocus() != null)
                            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                return false;
            }
        });
        mBinding.search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())){
                    mBinding.cancel.setVisibility(View.VISIBLE);
                }else {
                    mBinding.cancel.setVisibility(View.GONE);
                }
            }
        });
        mBinding.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBinding.search.getText().clear();
                //mBinding.cancel.setVisibility(View.GONE);
                kewWord = "";
            }
        });

        if (mAdapter == null){
            mAdapter = new PopualSearchAdapter(R.layout.item_search_resout);
            mAdapter.setItemClickListener(new BasicRecyViewHolder.OnItemClickListener() {
                @Override
                public void OnItemClick(View v, int adapterPosition) {
                    SearchResultBean bean = mAdapter.getData(mAdapter.getPositon(adapterPosition));
                    ARouter.getInstance().build("/curriculum/detail").withString("id",bean.getCourseId())
                            .withString("url",bean.getUrl()).navigation();
//                    ARouter.getInstance().build("/search/detail").withString("id",bean.getFlfg_id())
//                            .withString("title",bean.getFlfg_name())
//                            .navigation();
                }
            });
        }
        RecyclerView.LayoutManager mLinearment = new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        mBinding.recycler.setLayoutManager(mLinearment);
        mBinding.recycler.setAdapter(mAdapter);
        mBinding.recycler.addItemDecoration(new CustomDivider(mContext));

        mBinding.refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                index ++;
               // mPresenter.getSearchList(index,15,kewWord);
                mPresenter.getPopueSearchList(index,kewWord);

            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mBinding.llPopual.setVisibility(View.GONE);
                mBinding.llResult.setVisibility(View.VISIBLE);
                index =1;
                //mPresenter.getSearchList(index,15,kewWord);
                mPresenter.getPopueSearchList(index,kewWord);
            }
        });




    }

    @Override
    public void returnNoData() {
//        mAdapter.refreshDatas(new List<SearchResultBean>() {
//        });

        List<SearchResultBean> searchResultBeans=new ArrayList<>();
        mAdapter.refreshDatas(searchResultBeans);
        mBinding.refreshLayout.finishRefresh();
        mBinding.ivEmpty.setVisibility(View.VISIBLE);
    }

    @Override
    public void returnEsList(EsSearchResultBean list) {
//        mBinding.ivEmpty.setVisibility(View.GONE);
//        mBinding.refreshLayout.finishRefresh();
//        mBinding.refreshLayout.setLoadmoreFinished(false);
//        mAdapter.refreshDatas(list.getList());
    }

    @Override
    public void returnEsMoreList(EsSearchResultBean list) {
//        mBinding.ivEmpty.setVisibility(View.GONE);
//        mBinding.refreshLayout.finishLoadmore();
//        mBinding.refreshLayout.setLoadmoreFinished(false);
//        mAdapter.appendDatas(list);
    }


    @Override
    public void returnList(List<SearchResultBean> list) {
        mBinding.ivEmpty.setVisibility(View.GONE);
        mBinding.refreshLayout.finishRefresh();
        mBinding.refreshLayout.setLoadmoreFinished(false);
        mAdapter.refreshDatas(list);
    }

    @Override
    public void returnMoreList(List<SearchResultBean> list) {
        mBinding.ivEmpty.setVisibility(View.GONE);
        mBinding.refreshLayout.finishLoadmore();
        mBinding.refreshLayout.setLoadmoreFinished(false);
        mAdapter.appendDatas(list);
    }

    @Override
    public void returnNoMoreData() {
        mBinding.ivEmpty.setVisibility(View.GONE);
        mBinding.refreshLayout.finishLoadmore();
        mBinding.refreshLayout.setLoadmoreFinished(true);
    }

    @Override
    public void returnError() {
        mBinding.ivEmpty.setVisibility(View.GONE);
        if (index==1){
            mBinding.refreshLayout.finishRefresh();
        }else {
            mBinding.refreshLayout.finishLoadmore();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }
}
